﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class UIGSDAT : ViewBase
    {
        public TDRAUFTR cDR
        {
            get
            {
                return (TDRAUFTR)this.FindName("cDR");
            }
        }
        public VGSDAT TMV
        {
            get
            {
                return (VGSDAT)MV;
            }
        }
        
        public UIGSDAT()
        {

        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {

            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
        }
        public virtual void ImportExcel(object sender, RoutedEventArgs e)
        {

        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);

            if (dwMaster != null)
            {
                KEY = "G" + TMV.GSART + TMV.GSVAR;
                FERTKZ = TMV.GSFERTKZ;
                COUNTTYP = TMV.GSNUMART;
                GSKGPR = TMV.GSBWPR1;
                if (TMV.GSFERTKZ == "X" || FERTKZ == "F" || FERTKZ == "E")
                {
                    IsAutoGC = true;
                }
            }
            else
            {
                IsAutoGC = true;
                KEY = "";
                COUNTTYP = "";
            }
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                IsNew = true;
                ART = "";
                VAR = "  ";
                FARBE = "";
                return;
            }
            if (FARBE.Trim().Length == 0)
            {
                FARBE = "Z";
                Tracer.Info("纱线建立","默认供应类型: 外购"); 
            }
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["GSART"] = ART.Trim().PadRight(20);
            dwData["GSVAR"] = VAR.Trim().PadRight(3);
            dwData["GSFERTKZ"] = FARBE.Trim().PadRight(1);
            if (IsSJH)
            {
                dwData["GSNUMART"] = "DEN".PadRight(4);
                dwData["GSRECHNUM"] = "DEN".PadRight(4);
            }
            TBMain.Rows.InsertAt(dwData, 0);
            TMV.InsertGSDAT(dwData, app.company);
            IsNew = false;
            ART = "";
            VAR = "";
            FARBE = "";
            vwMaster.View.MoveCurrentToFirst();
            base.NewCommand(sender, e);
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {

            foreach (DataRow row in TBMain.Rows)
            {
                if (row.RowState == DataRowState.Unchanged)
                    continue; 
                dwMaster["GSVRPR"] = MV.date;
                TMV.UpdateGSDAT(row, 0);
            }

            base.SaveCommand(sender, e);
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                return;
            }
            filter = "GSART='" + dwMaster["GSART"].ToString() + "' AND GSVAR='" + dwMaster["GSVAR"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                TMV.DGS01(row);
                row.Delete(); 
            }
            TBMain.AcceptChanges();
            vwMaster.View.MoveCurrentToFirst();
            base.DeleteCommand(sender, e);
        }
        public virtual void LOAD()
        {
            TBMain.Clear();
            if (CART.Length < 20)
                return;
            PM["CART"] = CART;
            ((VGSDAT)MV).LOAD(PM);
            vwMaster.View.MoveCurrentToFirst();
        }
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                MessageBox.Show("清先选择一个记录");
                return;
            }
            if (!IsCopy)
            {
                IsCopy = true;
                ART = "";
                VAR = dwMaster["GSVAR"].ToString();
                FARBE = dwMaster["GSFERTKZ"].ToString();
                return;
            }
            if (VAR == "0")
            {
                MessageBox.Show("清先输入颜色数");
                return;
            }
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            dwData["GSART"] = ART.Trim().PadRight(20);
            dwData["GSVAR"] = VAR.Trim().PadRight(3);
            dwData["GSFERTKZ"] = FARBE.Trim().PadRight(1);
            TBMain.Rows.InsertAt(dwData, 0);
            TMV.InsertGSDAT(dwData, app.company);
            IsCopy = false;
            ART = "";
            VAR = "";
            FARBE = "";
            vwMaster.View.MoveCurrentToFirst();
            base.CopyCommand(sender, e);
        }


        public void Insert(DataRow _dwGS)
        {
            _dwGS["GSNEUBEN"] = app.User;
            _dwGS["GSNEUDAT"] = MV.date;

            ((VGSDAT)MV).InsertGSDAT(_dwGS, app.company);
            TBMain.Rows.InsertAt(_dwGS, 0);
            vwMaster.View.MoveCurrentToFirst();
        }
        
    }
    public partial class UIGSDAT
    {
        public virtual void OnFERTKZChg()
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (FERTKZ.Length < 1)
                return;
            TMV.GSFERTKZ = FERTKZ;
        }
        public virtual void OnCOUNTTYPChg()
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (COUNTTYP.Length < 4)
                return;
            TMV.GSNUMART = COUNTTYP;
            TMV.GSRECHNUM = COUNTTYP; 
        }

        #region ==CART===========
        public static readonly DependencyProperty CARTProperty = DependencyProperty.Register("CART",
            typeof(string), typeof(UIGSDAT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCARTChanged));
        public string CART
        {
            get
            {
                return (string)GetValue(CARTProperty);
            }
            set
            {
                SetValue(CARTProperty, value);
                LOAD();
            }
        }
        private static void OnCARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGSDAT;
            if (src != null)
            {
                src.CART = (string)e.NewValue;
            }
        }
        #endregion

        #region ==COUNTTYP===========
        public static readonly DependencyProperty COUNTTYPProperty = DependencyProperty.Register("COUNTTYP",
            typeof(string), typeof(UIGSDAT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCOUNTTYPChanged));
        public string COUNTTYP
        {
            get
            {
                return (string)GetValue(COUNTTYPProperty);
            }
            set
            {
                SetValue(COUNTTYPProperty, value);
                OnCOUNTTYPChg();
            }
        }
        private static void OnCOUNTTYPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGSDAT;
            if (src != null)
            {
                src.COUNTTYP = (string)e.NewValue;
            }
        } 
        #endregion
        #region ==FERTKZ===========
        public static readonly DependencyProperty FERTKZProperty = DependencyProperty.Register("FERTKZ",
            typeof(string), typeof(UIGSDAT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFERTKZChanged));
        public string FERTKZ
        {
            get
            {
                return (string)GetValue(FERTKZProperty);
            }
            set
            {
                SetValue(FERTKZProperty, value);
                OnFERTKZChg();
            }
        }
        private static void OnFERTKZChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGSDAT;
            if (src != null)
            {
                src.FERTKZ = (string)e.NewValue;
            }
        }
        
        #endregion


        #region ==GSKGPR===========
        public static readonly DependencyProperty GSKGPRProperty = DependencyProperty.Register("GSKGPR",
        typeof(double), typeof(UIGSDAT),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnGSKGPRChanged));
        public double GSKGPR
        {
            get
            {
                return (double)GetValue(GSKGPRProperty);
            }
            set
            {
                SetValue(GSKGPRProperty, value);

            }
        }
        private static void OnGSKGPRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGSDAT;
            if (src != null)
            {
                src.GSKGPR = (double)e.NewValue;
                src.OnGSKGPRChg((double)e.NewValue);
            }
        }
        public virtual void OnGSKGPRChg(double value)
        {
            if (TMV == null)
                return;
            TMV.GSBWPR1 = value;
            TMV.GSBWPR2 = Helper.RoundX(TMV.GSBWPR1 / 1.17 * 1.02, 1);
            TMV.GSBWPR3 = Helper.RoundX(TMV.GSBWPR1 / 1.17 * 1.04, 1);
        }
        #endregion

        #region ==dwGS===========
        public static readonly DependencyProperty dwGSProperty = DependencyProperty.Register("dwGS",
            typeof(DataRow), typeof(UIGSDAT),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwGSChanged));
        public DataRow dwGS
        {
            get
            {
                return (DataRow)GetValue(dwGSProperty);
            }
            set
            {
                SetValue(dwGSProperty, value);
            }
        }
        private static void OndwGSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGSDAT;
            if (src != null)
            {
                src.dwGS = (DataRow)e.NewValue;
            }
        }
        #endregion

        #region ==FTGS04===========
        public static readonly DependencyProperty FTGS04Property = DependencyProperty.Register("FTGS04",
            typeof(bool), typeof(UIGSDAT),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFTGS04Changed));
        public bool FTGS04
        {
            get
            {
                return (bool)GetValue(FTGS04Property);
            }
            set
            {
                SetValue(FTGS04Property, value);
                ((VGSDAT)MV).FTGS04 = value;
            }
        }
        private static void OnFTGS04Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGSDAT;
            if (src != null)
            {
                src.FTGS04 = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==IsAutoGS===========
        public static readonly DependencyProperty IsAutoGSProperty = DependencyProperty.Register("IsAutoGS",
            typeof(bool), typeof(UIGSDAT),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsAutoGSChanged));
        public bool IsAutoGS
        {
            get
            {
                return (bool)GetValue(IsAutoGSProperty);
            }
            set
            {
                SetValue(IsAutoGSProperty, value);
            }
        }
        private static void OnIsAutoGSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGSDAT;
            if (src != null)
            {
                src.IsAutoGS = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==IsAutoGC===========
        public static readonly DependencyProperty IsAutoGCProperty = DependencyProperty.Register("IsAutoGC",
            typeof(bool), typeof(UIGSDAT),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsAutoGCChanged));
        public bool IsAutoGC
        {
            get
            {
                return (bool)GetValue(IsAutoGCProperty);
            }
            set
            {
                SetValue(IsAutoGCProperty, value);
            }
        }
        private static void OnIsAutoGCChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGSDAT;
            if (src != null)
            {
                src.IsAutoGC = (bool)e.NewValue;
            }
        }
        #endregion
    }
}
